package com.yzt.dynamicmodule.activity;

import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.yzt.zhuju.mvp.CommonPresenter;
import com.yzt.zhuju.mvp.MVPActivity;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.dynamic.GameScriptBaseBean;
import com.yzt.zhuju.network.entity.dynamic.GameScriptBean;
import com.yzt.zhuju.widget.refreshHeader.WaterDropHeader;
import com.yzt.dynamicmodule.R;
import com.yzt.dynamicmodule.adapter.GameScriptAdapter;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.List;

/**
 * 作者：bobwang
 * 创建时间：2023-10-12 00:14:20
 * 描述：游戏剧本列表
 */
public class GameScriptListActivity extends MVPActivity<CommonPresenter> implements OnRefreshListener {

    private String extraId;

    private boolean isHost;

    private GameScriptAdapter adapter;

    private RefreshLayout mRefreshLayout;

    @Override
    public int getLayoutId() {
        return R.layout.activity_game_script;
    }

    @Override
    public void installViews() {
        isHost = getIntent().getBooleanExtra("extra_host", false);
        extraId = getIntent().getStringExtra("extra_id");
        mTitleBar.setTitle("游戏剧本");
//        if (isHost) {
//            mTitleBar.setRightContent("确定");
//            mTitleBar.setRightTextColor(ContextCompat.getColor(this, R.color.color_FF3C60));
//            mTitleBar.setRightBtnListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    GameScriptBean bean = null;
//                    for (GameScriptBean entry : adapter.getData()) {
//                        if (entry.isChecked()) {
//                            bean = entry;
//                        }
//                    }
//                    if (bean == null) {
//                        return;
//                    }
//                    Intent intent = new Intent();
//                    intent.putExtra("extra_data", bean);
//                    setResult(RESULT_OK, intent);
//                    finish();
//                }
//            });
//        }
        mRefreshLayout = findViewById(R.id.refreshLayout);
        mRefreshLayout.setRefreshHeader(new WaterDropHeader(mActivity));
        mRefreshLayout.setOnRefreshListener(this);

        RecyclerView recycle = findViewById(R.id.blacklist_rv);
        recycle.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new GameScriptAdapter();
        adapter.setHost(isHost);
        recycle.setAdapter(adapter);

        adapter.setOnItemClickListener((adapter, view, position) -> {
            GameScriptBean entry = (GameScriptBean) adapter.getItem(position);
            Intent intent = new Intent(GameScriptListActivity.this, GameScriptDetailActivity.class);
            intent.putExtra("extra_id", entry.getGameId());
            intent.putExtra("extra_status", entry.isUsing());
            intent.putExtra("extra_host",isHost);
            startActivityForResult(intent, 1);
        });

        adapter.addChildClickViewIds(R.id.img_check);
        adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter1, @NonNull View view, int position) {
                if (view.getId() == R.id.img_check) {
                    for (GameScriptBean entry : adapter.getData()) {
                        entry.setChecked(false);
                    }
                    adapter.getItem(position).setChecked(true);
                    adapter.notifyDataSetChanged();
                }
            }
        });

        adapter.getLoadMoreModule().setOnLoadMoreListener(() -> {
            mCurrentPage++;
            getHttpData();
        });

        mRefreshLayout.autoRefresh();
    }

    /**
     * 选择剧本
     */
    public void getHttpData() {

        BizLayer.getInstance().getDynamicModule().scriptList(extraId, mCurrentPage, PAGE_SIZE, new AresSubscriber<GameScriptBaseBean>() {
            @Override
            protected void onSuccess(GameScriptBaseBean bean) {
                List<GameScriptBean> list = bean.getList();
                if (mCurrentPage == 1) {
                    if (list.size() == 0) {
                        adapter.setEmptyView(getEmptyView());
                    } else {
                        adapter.removeEmptyView();
                    }
                    adapter.setList(list);
                } else {
                    adapter.addData(list);
                }
                adapter.getLoadMoreModule().loadMoreComplete();
                if (list.size() < PAGE_SIZE) {
                    adapter.getLoadMoreModule().loadMoreEnd();
                }
            }

            @Override
            protected void onError(int code, String info) {
                super.onError(code, info);
                if (mCurrentPage == 1) {
                    adapter.setEmptyView(getErrorView(info));
                }
            }

            @Override
            protected void onFinallyEnd() {
                super.onFinallyEnd();
                mRefreshLayout.finishRefresh();
            }
        });
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mCurrentPage = 1;
        getHttpData();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            int id = data.getIntExtra("extra_id", 0);
            GameScriptBean bean = null;
            for (GameScriptBean entry : adapter.getData()) {
                if (entry.getGameId() == id) {
                    bean = entry;
                }
            }
            if (bean == null) {
                return;
            }
            Intent intent = new Intent();
            intent.putExtra("extra_data", bean);
            setResult(RESULT_OK, intent);
            finish();

        }
    }
}